import java.util.ArrayList;
import java.util.Scanner;
//fork pr
class Record {
    String date;
    double amount;
    String category;
    String note;

    public Record(String date, double amount, String category, String note) {
        this.date = date;
        this.amount = amount;
        this.category = category;
        this.note = note;
    }

    @Override
    public String toString() {
        return "日期: " + date + ", 金额: " + amount + ", 类别: " + category + ", 备注: " + note;
    }
}

public class PersonalFinanceManager {
    private static ArrayList<Record> incomeRecords = new ArrayList<>();
    private static ArrayList<Record> expenseRecords = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);
    private static double monthlyBudget = 0.0;

    public static void main(String[] args) {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice) {
                case 1:
                    addIncome();
                    break;
                case 2:
                    addExpense();
                    break;
                case 3:
                    viewRecords();
                    break;
                case 4:
                    queryRecords();
                    break;
                case 5:
                    manageBudget();
                    break;
                case 6:
                    monthlyStatistics();
                    break;
                case 7:
                    System.out.println("退出系统。");
                    return;
                default:
                    System.out.println("无效的选项，请重新选择。");
            }
        }
    }

    private static void showMenu() {
        System.out.println("个人账单管理系统");
        System.out.println("1. 记录收入");
        System.out.println("2. 记录支出");
        System.out.println("3. 查看所有账单");
        System.out.println("4. 查询账单");
        System.out.println("5. 预算管理");
        System.out.println("6. 月度统计");
        System.out.println("7. 退出");
        System.out.print("请选择操作: ");
    }

    private static void addIncome() {
        System.out.print("输入收入日期 (YYYY-MM-DD): ");
        String date = scanner.nextLine();
        System.out.print("输入收入金额: ");
        double amount = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符
        System.out.print("输入收入类别: ");
        String category = scanner.nextLine();
        System.out.print("输入备注: ");
        String note = scanner.nextLine();

        if (amount <= 0) {
            System.out.println("收入金额必须为正数。");
            return;
        }

        incomeRecords.add(new Record(date, amount, category, note));
        System.out.println("收入记录成功！");
    }

    private static void addExpense() {
        System.out.print("输入支出日期 (YYYY-MM-DD): ");
        String date = scanner.nextLine();
        System.out.print("输入支出金额: ");
        double amount = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符
        System.out.print("输入支出类别: ");
        String category = scanner.nextLine();
        System.out.print("输入备注: ");
        String note = scanner.nextLine();

        if (amount <= 0) {
            System.out.println("支出金额必须为正数。");
            return;
        }

        expenseRecords.add(new Record(date, amount, category, note));
        System.out.println("支出记录成功！");
    }
    private static void viewRecords() {
        System.out.println("收入记录:");
        for (Record record : incomeRecords) {
            System.out.println(record);
        }

        System.out.println("支出记录:");
        for (Record record : expenseRecords) {
            System.out.println(record);
        }
    }

    private static void queryRecords() {
        System.out.println("查询账单");
        System.out.println("1. 按日期查询");
        System.out.println("2. 按日期范围查询");
        System.out.println("3. 按类别查询");
        System.out.print("请选择查询方式: ");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        switch (choice) {
            case 1:
                queryByDate();
                break;
            case 2:
                queryByDateRange();
                break;
            case 3:
                queryByCategory();
                break;
            default:
                System.out.println("无效的选项，请重新选择。");
        }
    }

    private static void queryByDate() {
        System.out.print("输入查询日期 (YYYY-MM-DD): ");
        String date = scanner.nextLine();
        System.out.println("收入记录:");
        for (Record record : incomeRecords) {
            if (record.date.equals(date)) {
                System.out.println(record);
            }
        }
        System.out.println("支出记录:");
        for (Record record : expenseRecords) {
            if (record.date.equals(date)) {
                System.out.println(record);
            }
        }
    }

    private static void queryByDateRange() {
        System.out.print("输入起始日期 (YYYY-MM-DD): ");
        String startDate = scanner.nextLine();
        System.out.print("输入结束日期 (YYYY-MM-DD): ");
        String endDate = scanner.nextLine();
        System.out.println("收入记录:");
        for (Record record : incomeRecords) {
            if (record.date.compareTo(startDate) >= 0 && record.date.compareTo(endDate) <= 0) {
                System.out.println(record);
            }
        }
        System.out.println("支出记录:");
        for (Record record : expenseRecords) {
            if (record.date.compareTo(startDate) >= 0 && record.date.compareTo(endDate) <= 0) {
                System.out.println(record);
            }
        }
    }

    private static void queryByCategory() {
        System.out.print("输入查询类别: ");
        String category = scanner.nextLine();
        System.out.println("收入记录:");
        for (Record record : incomeRecords) {
            if (record.category.equals(category)) {
                System.out.println(record);
            }
        }
        System.out.println("支出记录:");
        for (Record record : expenseRecords) {
            if (record.category.equals(category)) {
                System.out.println(record);
            }
        }
    }


    private static void manageBudget() {
        System.out.print("设置每月支出预算限额: ");
        monthlyBudget = scanner.nextDouble();
        scanner.nextLine(); // 消耗换行符
        System.out.println("预算设置成功！");
    }

    private static void monthlyStatistics() {
        double totalIncome = 0.0;
        double totalExpense = 0.0;

        for (Record record : incomeRecords) {
            totalIncome += record.amount;
        }

        for (Record record : expenseRecords) {
            totalExpense += record.amount;
        }

        System.out.println("本月总收入: " + totalIncome);
        System.out.println("本月总支出: " + totalExpense);
        System.out.println("本月剩余预算: " + (monthlyBudget - totalExpense));

        System.out.println("各类别支出:");
        for (Record record : expenseRecords) {
            System.out.println(record.category + ": " + record.amount);
        }
    }
}
